package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ConstructorDemo02 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<Rectangle> rectangleClass = Rectangle.class;
        //获取指定构造方法，包括私有方法
        Constructor<Rectangle> constructor =
                rectangleClass.getDeclaredConstructor(int.class,int.class);
        //通过反射构建对象实例
        constructor.setAccessible(true);//设置私有方法可访问
        Rectangle rectangle =constructor.newInstance(10,20);
        //获取矩形周长
        int tPerimeter=rectangle.getPerimeter();
        System.out.println("矩形周长："+tPerimeter);
    }
}
